package com.weave.view;

import com.weave.model.GameModel;

import javax.swing.*;
import java.awt.*;
import java.util.Observable;
import java.util.Observer;

public class GameFrame extends JFrame implements Observer {
    // 成员变量声明
    private final GameModel model;
    private final WordGridPanel wordGrid;
    private final KeyboardPanel keyboard;
    private final JButton resetButton;
    private final JLabel inputPreview; // 新增输入预览标签
    private final JPanel controlPanel;
    private final JButton newGameButton;

    public JButton getNewGameButton() {
        return newGameButton;
    }

    public GameModel getModel() {
        return model;
    }

    public WordGridPanel getWordGrid() {
        return wordGrid;
    }

    public JButton getResetButton() {
        return resetButton;
    }

    public JLabel getInputPreview() {
        return inputPreview;
    }

    public JPanel getControlPanel() {
        return controlPanel;
    }

    public GameFrame(GameModel model) {
        setTitle("WEAVE");
        this.model = model;
        model.addObserver(this);

        // 初始化所有UI组件
        inputPreview = new JLabel("", SwingConstants.CENTER);
        wordGrid = new WordGridPanel(model);
        keyboard = new KeyboardPanel();
        resetButton = new JButton("Reset");
        newGameButton = new JButton("New Game");
        controlPanel = new JPanel();

        setupUI();
    }

    private void setupUI() {
        setLayout(new BorderLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);

        configureControlPanel();

        addInputPreview();

        add(controlPanel, BorderLayout.NORTH);
        add(wordGrid, BorderLayout.CENTER);
        add(keyboard, BorderLayout.SOUTH);

        setMinimumSize(new Dimension(600, 800));
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    private void configureControlPanel() {
        resetButton.setEnabled(false);


        controlPanel.add(resetButton);
        controlPanel.add(newGameButton);
    }

    private void addInputPreview() {
        JPanel previewPanel = new JPanel(new BorderLayout());
        previewPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        JLabel titleLabel = new JLabel("Current input：");
        titleLabel.setFont(new Font("宋体", Font.BOLD, 14));

        inputPreview.setFont(new Font("宋体", Font.BOLD, 24));
        inputPreview.setBorder(BorderFactory.createLineBorder(Color.GRAY));

        previewPanel.add(titleLabel, BorderLayout.NORTH);
        previewPanel.add(inputPreview, BorderLayout.CENTER);

        add(previewPanel, BorderLayout.EAST);
    }

    public void setInputPreview(String text) {
        StringBuilder display = new StringBuilder();
        for (int i = 0; i < GameModel.WORD_LENGTH; i++) {
            if (i < text.length()) {
                display.append(text.charAt(i));
            } else {
                display.append("_");
            }
            display.append(" ");
        }
        inputPreview.setText(display.toString().trim());
    }

    public KeyboardPanel getKeyboard() {
        return keyboard;
    }

    @Override
    public void update(Observable o, Object arg) {
        wordGrid.repaint();
        resetButton.setEnabled(model.getSteps().size() > 1);

        if (model.isGameWon()) {
            JOptionPane.showMessageDialog(this,
                    "Success! Steps：" + (model.getSteps().size()-1),
                    "Game victory",
                    JOptionPane.INFORMATION_MESSAGE
            );
        }
    }
}